package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.mapper.UserMapper;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("collect")
public class CollectController {
    @Autowired
    private IUserService userService;
    @Autowired
    private IStrategyService strategyService;
    /**
     * 通过传入的游记id和用户id查找是否收藏
     * @param userId
     * @param travelId
     * @return
     */
    @GetMapping("{userId}/{travelId}")
    public boolean listCollectStatus(@PathVariable Long userId, @PathVariable Long travelId){
        boolean type = userService.selectCollectByUserId(userId,travelId);
        return type;
    }

    /**
     * 通过传入的用户id和游记的id保存收藏关系
     * @param userId
     * @param travelId
     * @return
     */
    @PostMapping
    public JSONResult saveTravelCollect( Long userId,  Long travelId){
        userService.saveCollectByUserId(userId,travelId);
        return new JSONResult();
    }

    /**
     * 通过传入的用户id和游记id删除收藏关系
     * @param userId
     * @param travelId
     * @return
     */
    @DeleteMapping
    public JSONResult deleteTravelCollect( Long userId, Long travelId){
        userService.deleteCollectByUserId(userId,travelId);
        return new JSONResult();
    }

    /**
     * 根据攻略id和用户id来保存收藏
     * @param userId
     * @param strategyId
     * @return
     */
    @PostMapping("strategy")
    public JSONResult saveStrategyCollect ( Long userId,  Long strategyId){
        strategyService.saveCollectByStrategyId(userId,strategyId);
        return new JSONResult();
    }

    /**
     * 通过传入的用户id和游记id删除对应的收藏关系
     * @param userId
     * @param strategyId
     * @return
     */
    @DeleteMapping("strategy")
    public JSONResult deleteStrategyCollect(Long userId, Long strategyId){
        strategyService.deleteCollectByStrategyId(userId,strategyId);
        return new JSONResult();
    }

    /**
     * 通过传入的攻略id查询数据的总条数
     * @param strategyId
     * @return
     */
    @GetMapping("strategy/count")
    public int selectCollectCountByStrategyId(Long strategyId){
        int collectCount = strategyService.selectCollectCountByStrategyId(strategyId);
       return collectCount;
    }

    /**
     * 通过用户的id和攻略的id判断是否收藏当前攻略
     * @param userId
     * @param strategyId
     * @return
     */
    @GetMapping("strategy")
    public boolean  selectCollectByStrategyId(Long userId, Long strategyId){
        boolean type = strategyService.selectCollectByStrategyId(userId,strategyId);
        return type;
    }
}
